package com.ibm.productivity.tools.blogplugin;

import com.ibm.productivity.tools.blogplugin.meta.Article;
import com.ibm.productivity.tools.blogplugin.meta.Comment;
import com.ibm.productivity.tools.blogplugin.meta.IBlog;
import com.ibm.productivity.tools.blogplugin.meta.IBlogAccount;
import com.ibm.productivity.tools.blogplugin.views.ArticleNode;
import com.ibm.productivity.tools.blogplugin.views.ArticleParent;

public class BlogDoc {
	IBlogAccount blogAccout;
	ArticleParent[] roots = null;
	EditingViewList viewlist = null;
	String[] blogNames;
	String author = null;
	boolean isLogin;

	public ArticleParent[] getRoot() {
		return roots;
	}

	public void addNewNode(int blogId, Article a) {
		if (roots == null)
			return;
		ArticleParent node = new ArticleParent(a.getTitle(), -1
				* roots[blogId].getChildCount());
		node.setArticle(a);
		roots[blogId].addChild(node);
	}

	public boolean getIsLogin() {
		return isLogin;
	}

	public BlogDoc(IBlogAccount blogAccout) {
		isLogin = false;
		this.blogAccout = blogAccout;
		viewlist = new EditingViewList();
	}

	public EditingViewList getViewList() {
		return viewlist;
	}

	public IBlogAccount getBlogAccount() {
		return blogAccout;
	}

	public ArticleParent[] getArticleTree() {
		if (roots == null) {
			return buildArticleRoot();
		}
		return roots;
	}

	public boolean login(String username, String password) {
		isLogin = blogAccout.login(username, password);
		if (isLogin && author == null)
			setAuthor(username);
		//buildArticleRoot();
		return isLogin;
	}

	public void logout() {
		blogAccout.logout();
		roots = null;
		isLogin = false;
	}

	public ArticleParent[] buildArticleRoot() {
		try {
			if (isLogin == false)
				return null;
			if (blogAccout.getBlogCount() == 0)
				return null;

			// get roots count
			int n = blogAccout.getBlogCount();
			roots = new ArticleParent[n];
			blogNames = new String[n];
			for (int i = 0; i < n; ++i) {
				IBlog blog = blogAccout.getBlog(i);
				roots[i] = new ArticleParent(blog.getBlogName(), i + 1);
				blogNames[i] = new String(blog.getBlogName());
				Article[] article = blog.getArticles();
				for (int j = 0; j < article.length; ++j) {
					ArticleParent node = new ArticleParent(article[j]
							.getTitle(), j);
					node.setArticle(article[j]);

					if (blog.canListComment()) {
						Comment[] comments = blog.getComments(article[j]);
						if (comments != null) {
							for (int k = 0; k < comments.length; ++k) {
								ArticleNode commentNode = new ArticleNode(
										"Comment " + (k + 1),
										ArticleNode.COMMENTSTHRESHOLD + k);
								commentNode.setComment(comments[k]);
								node.addChild(commentNode);
							}
						}
					}
					roots[i].addChild(node);
				}
			}
			return roots;
		} catch (Exception e) {
			e.printStackTrace();
			logout();
			return null;
		}

	}

	public String[] getBlogNames() {
		return blogNames;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

}
